/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulation;

/**
 *
 * @author Danielondon
 */
public class Market implements IMarket{
    private double [] pricesPerSlot;
    private int slotCount;
    private int slotSize;
    private String currency;
    private double [] scalePrices;
    double scaleFactor;
    String name;
    private Market(String name, int slotCount, int slotSize, String currency) {
        this.name = name;
        this.slotCount = slotCount;
        this.slotSize = slotSize;
        this.currency = currency;
        this.scaleFactor = (double)this.slotSize/(double)CommonResources.DEFAULT_INTERVAL;
    }
    public Market(String name, double [] prices, int slotCount, int slotSize, String currency) throws Exception{
        this(name, slotCount, slotSize, currency);
        this.pricesPerSlot = prices;        
        if (this.slotCount != pricesPerSlot.length){
            throw new Exception ("Invalid Market Creation");
        }
    }
        
    public Market(String pricesConcatenation, String separator, int slotCount, int slotSize, String currency) throws Exception{
        this("", slotCount, slotSize, currency);
        String []tokens = pricesConcatenation.split(separator);
        if (tokens.length != (slotCount+1)) {
            throw new Exception("Invalid Market Parse Creation. Tokens size " + tokens.length + " and slot Count " + slotCount);
        }
        this.pricesPerSlot = new double[slotCount];        
        for (int i = 0; i < slotCount; i++){
            this.pricesPerSlot[i] = Double.parseDouble(tokens[i+1]);
        }
        
        this.name = tokens[0];
    }

    /**
     * @return the slotCount
     */
    public int getSlotCount() {
        return slotCount;
    }

    /**
     * @param slotCount the slotCount to set
     */
    public void setSlotCount(int slotCount) {
        this.slotCount = slotCount;
    }
    
 
    @Override
    public String toString(){
        String text = this.name+CommonResources.STRING_SEPARATOR;
        for (double d:this.pricesPerSlot){
            text +=  d + CommonResources.STRING_SEPARATOR;
        }
        
        return text;
    }

    @Override
    public double getPrice(int i) {
        return this.scalePrice(pricesPerSlot[i]);
    }

    @Override
    public double[] getAllPrices() {
        if (this.scalePrices == null){
            scalePrices = new double[pricesPerSlot.length];
            for (int i = 0; i < scalePrices.length; i++){
                scalePrices[i] = this.scalePrice(pricesPerSlot[i]);
            }
        }
        
        return scalePrices;
    }
    
    public double scalePrice(double price){
        return price * scaleFactor;
    }

    @Override
    public String getName() {
        return this.name;
    }
}
